import threading
import multiprocessing
from utils.name_generator import generator

class ExcutorThread(threading.Thread):

    def __init__(self, name=None, jobs=None):
        threading.Thread.__init__(self)
        self.name = name
        if name is None:
            self.name = generator()
        self.jobs = jobs

    def run(self):

        try:
            if self.jobs:
                for job in self.jobs:
                    if job.exc_func and job.exc_func_args:
                        job.exc_func(job.exc_func_args)
                        if job.next_func and job.next_func_args:
                            job.next_func(job.next_func_args)
                    else:
                        if job.exc_func and job.exc_func_args is None:
                            job.exc_func()
                            if job.next_func and job.next_func_args is None:
                                job.next_func()

                    if job.next_jobs:
                        ExcutorThread(jobs=job.next_jobs).run()
                    if job.call_back:
                        job.call_back()

        except Exception as e:
            print(f'error in excutor[{self.name}]:',e)

